package com.ruoyi.quartz.task;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.model.domain.AccountMonitoringQuery;
import com.ruoyi.system.model.domain.GoodsInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling
@Slf4j
public class MonitorTask {


    @Resource
    GoodsInfoMapper goodsInfoMapper;

//    @Scheduled(cron = "0 0 03 ? * WED")
////    @Scheduled(cron = "0/5 * * * * ?")
//    public void selectPushTask() {
//
//
//        try{
//            GoodsInfo goodsInfo = new GoodsInfo();
//            goodsInfo.setSaleState(1);
//            goodsInfo.setAccountType(1);
//            goodsInfo.setGameId(8l);
//            List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectGoodsInfoList(goodsInfo);
//            goodsInfoList = goodsInfoList.stream().filter(g -> Objects.equals("QQ",g.getCarrierName())).collect(Collectors.toList());
//
//            goodsInfo = new GoodsInfo();
//            goodsInfo.setSaleState(1);
//            goodsInfo.setAccountType(1);
//            goodsInfo.setGameId(5l);
//            List<GoodsInfo> lolGoodsInfoList = goodsInfoMapper.selectGoodsInfoList(goodsInfo);
//
//            goodsInfoList.addAll(lolGoodsInfoList);
//            goodsInfoList = goodsInfoList.stream().filter(g -> StringUtils.isNotEmpty(g.getAccountNumber()) && StringUtils.isNotEmpty(g.getSecurityPhone())).collect(Collectors.toList());
//
//            String Key = "PKrC7oXnxTqtyYWuSzcOJ3QMBVp4Hikd";
//            String Secret = "vckSi0zYBfLlIPT7Fp8KRgrOtmVb43Jj";
//            for (GoodsInfo info : goodsInfoList) {
//                if (!Objects.equals("2",info.getMonitoringState())){
//                    String body = HttpRequest.post("http://c.p74.cn/api/account/add")
//                            .form("account_number", info.getAccountNumber())
//                            .form("account_phone", info.getSecurityPhone())
//                            .form("check_interval", "1")
//                            .form("interval_type", "0")
//                            .form("task_type", "3")
//                            .form("auto_switch_type", "0")
//                            .form("remark", "0")
//                            .form("apiKey", Key)
//                            .form("apiSecret", Secret).execute().body();
//
//                    JSONObject jsonObject = JSONObject.parseObject(body);
//                    String code = jsonObject.getString("code");
//                    if (Objects.equals("1",code)){
//                        boolean b = true;
//                        while (b){
//                            JSONObject data = jsonObject.getJSONObject("data");
//                            String checkId = data.getString("checkId");
//                            String checkBody = HttpRequest.post("http://c.p74.cn/api/account/query")
//                                    .form("checkId", checkId)
//                                    .form("apiKey", Key)
//                                    .form("apiSecret", Secret).execute().body();
//
//                            JSONObject checkJson = JSONObject.parseObject(checkBody);
//                            JSONObject checkData = checkJson.getJSONObject("data");
//                            String status = checkData.getString("status");
//                            String taskStatus = checkData.getString("task_status");
//                            if (Objects.equals(taskStatus,2) || Objects.equals(taskStatus,3)){
//                                b = false;
//                                String bind_tel = checkData.getString("bind_tel");
//                                Integer id = checkData.getInteger("id");
//
//                                AccountMonitoringQuery accountMonitoringQuery = new AccountMonitoringQuery();
//
//                                if(Objects.equals("0",status)){
//                                    //账号未掉绑
//                                    info.setMonitoringState("1");
//                                    accountMonitoringQuery.setStatus("1");
//                                }else {
//                                    //账号掉绑
//                                    info.setMonitoringState("2");
//                                    accountMonitoringQuery.setStatus("2");
//                                }
//                                if(Objects.equals("4",status)){
//                                    accountMonitoringQuery.setStatus("4");
//                                }
//
//                                goodsInfoMapper.updateGoodsInfo(info);
//
//                                accountMonitoringQuery.setCreateId(info.getCreateId());
//                                accountMonitoringQuery.setCreateTime(new Date());
//                                accountMonitoringQuery.setMsg(checkBody);
//                                accountMonitoringQuery.setQueryId(id);
//                                accountMonitoringQuery.setQq(info.getAccountNumber());
//                                accountMonitoringQuery.setTelephone(info.getSecurityPhone());
//
//                                accountMonitoringQuery.setBindTel(bind_tel);
//                                accountMonitoringQuery.setAccountMonitoringId(info.getId());//商品ID
//                                accountMonitoringQueryService.save(accountMonitoringQuery);
//                            }
//                        }
//                    }
//                }
//            }
//        }catch (Exception e){
//            System.out.println(e.getMessage());
//        }
//    }
}
